<?php
require_once('include/inc_database.php');
require_once('include/inc_useraccount.php');
require_once('include/inc_recommendation.php');

// Open DB connection.
$dbobj = new DatabaseObject();
$dbobj->OpenConnection();

$auth = new UserAccount();

if ($auth->AuthenticatedUser() == "") {
	Header("Location: login.php");
	exit();
}

//echo realpath($_SERVER['DOCUMENT_ROOT']);

$username = $auth->AuthenticatedUser();

//-------
// Update
//-------
if ($_POST['update_user'] == "on") {
	$username = trim($_POST['username']);
	$password = $_POST['password'];
	$password2 = $_POST['password2'];
	$email = substr(trim($_POST['email']), 0, 100);
	if ($_POST['emailNewRecs'] == "on")
		$emailNewRecs = true;
	else
		$emailNewRecs = false;
	$fullName = substr(trim($_POST['fullName']), 0, 50);
	$url = substr(trim($_POST['url']), 0, 100);

	$dbobj->SendQuery("UPDATE mr_useraccount SET email = " . ValToSQL_str($email) . ", emailNewRecs = " . ValToSQL_bit($emailNewRecs) . ", fullName = " . ValToSQL_str($fullName) . ", url = " . ValToSQL_str($url) . " WHERE username = " . ValToSQL_str($username) . "");

	//--------------------------------
	// Deal with uploaded icon35 file.
	//--------------------------------
	if ($_FILES['userIcon']['name'] <> "") {

		//echo $_FILES['userIcon']['tmp_name'];

		//echo file_exists($_FILES['userIcon']['tmp_name']) + 0;

		// Check for upload error.
		if ($_FILES['userIcon']['error'] <> UPLOAD_ERR_OK) {
			echo "<p>Upload error code " . $_FILES[$upload_form_name]['error'] ."<br>See http://us3.php.net/manual/en/features.file-upload.errors.php";
			exit();
		}

		$auth->UpdateUserIcon($username, $_FILES['userIcon']['tmp_name']);

	}

	// Refresh $auth.
	$auth = new UserAccount();
}
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?=SiteConst('site_name')?></title>

<link rel="shortcut icon" href="images/favicon.gif" type="image/x-icon">
<meta name="viewport" content="width=700"/>

<style type="text/css">

	@import url('rec.css?<?=$CSS_VERSION?>');

	/* form */
	tr.rec_form_row > td {
		padding-bottom: 8px;
	}

	tr.rec_form_row .info_only {
		color: #888;
		font-style: italic;
	}

	tr.rec_form_row .required {
		font-weight: bold;
	}


</style>
</head>

<body>

<div id="main">

<h1 style="margin-bottom:10px;">
	<a href="./"><img src="images/logo02b.jpg" align="middle" border="0"/></a>
	Account Settings
</h1>


<?if ($_POST['update_user'] == "on") {?>
	<div class="inset_box">
		<img src="images/success_check01.png" align="top"/> Account updated.&nbsp; Return to <a href="./"><?=SiteConst('site_name')?></a>.
	</div>
<?}?>
	
<div class="inset_box">
<form method="post" action="user_settings.php" enctype="multipart/form-data">
	<input type="hidden" name="update_user" VALUE="on">
	<input type="hidden" name="username" VALUE="<?=$username?>">

	<table style="width:100%;">
	<tr class="rec_form_row">
		<td class="required">Username:</td>
		<td><?=$username?></td>
	</tr>
	<tr class="rec_form_row">
		<td class="required">Email:</td>
		<td>
			<input type="text" name="email" value="<?=$auth->user_r['email']?>" size="30">
			<div class="info_only">For password reset, and other essentials, when such systems exist.</div>
		</td>
	</tr>
	<tr class="rec_form_row">
		<td></td>
		<td>
			<input type="checkbox" name="emailNewRecs" <?=($auth->user_r['emailNewRecs'] ? "checked" : "")?>> Email me subscription feed updates
			<div class="info_only" style="margin-left:25px;">Recommended, unless using your <a href="rec_feed.php?user_in=<?=$username?>">rss feed</a> instead.</div>
		</td>
	</tr>
	<tr class="rec_form_row">
		<td>My name (optional):</td>
		<td>
			<input type="text" name="fullName" value="<?=$auth->user_r['fullName']?>" size="20">
		</td>
	</tr>
	<tr class="rec_form_row">
		<td>My homepage (optional):</td>
		<td>
			<input type="text" name="url" value="<?=$auth->user_r['url']?>" size="50">
			<div class="info_only">Must begin with http://, etc.</div>
		</td>
	</tr>
	<tr class="rec_form_row">
	<td>User icon:</td>
	<td>
		<?if (!empty($auth->user_r['userIconPath'])) {?>
			<img src="image.php?usericon=<?=$auth->user_r['userIconPath']?>"/>
		<?}
		else {?>
			<i>Icon not uploaded yet.</i>
		<?}?>
	    <br/>Upload new icon: <input name="userIcon" type="file" />
		<br/>
		<span class="info_only">(Must be a JPEG file.  May need to shift-reload to overwrite browser cached copy.)</span>
	</td>
	</tr>	<tr class="rec_form_row">
		<td></td>
		<td>
			<input type="submit" name="submit" value="Update account">
		</td>
	</tr>
	</table>

</form>
</div>

<hr />

<div style="text-align:center; margin:20px auto;">
	Return to <a href="./"><?=SiteConst('site_name')?></a>
	<br/><br/><a href="http://code.google.com/p/mediarecommend/"><img src="images/poweredby_mediarecommend.png" style="border-width:0;"/></a>
</div>

</div>


</body>
</html>
